package com.yc.Testjvm.classload.simple;

import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Date;

public class JavaClassExecuter {

	//存储中间输出打印数据
	private static StringBuffer sb = new StringBuffer();
	private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	
	//输出
	public static void println(Object obj) {
		sb.append("\n" + sdf.format(new Date()) + " - " + obj.toString());
	}
	
	public static String getPrint() {
		return sb.toString();
	}
	
	/**
	 * @param classBytes
	 * @param args
	 */
	public static void execute(byte[] classBytes) {
		sb.setLength(0);//清空历史数据
		
		SimpleLoader loader = new SimpleLoader();
		Class clazz = loader.loadBytes(classBytes);
		try {
			Method method = clazz.getMethod("main", new Class[] {String[].class});
			method.invoke(null, new String[] {null});
		} catch (Exception e) {
			sb.append("\n" + e.toString());
		}
		
	}
	
}
